#include "gamelogic.h"
#include"qipan.h"
#include<QDebug>
GameLogic::GameLogic(QObject *parent) : QObject(parent)
{

}

void GameLogic::setQipanBoard(Role **board, int boardSize)
{
   m_board=board;
   m_boardSize=boardSize;


}

bool GameLogic::checkWin(int row, int col, Role role)
{
    /*四个方向*/
    const int directions[4][2]=
    {
        /*水平*/
        {1,0},
        /*垂直*/
        {0,1},
        /*左对角*/
        {1,1},
        /*右对角*/
        {1,-1}
    };
    /*四个方向*/
    for(int direct=0;direct<4;direct++)
    {
        int cnt=1;
        //向一个方向的正方向探测
        for(int idx=1;idx<5;idx++)
        {
            int newRow=row+directions[direct][0]*idx;
            int newCol=col+directions[direct][1]*idx;
            if(newRow>=0&&newRow<m_boardSize&&newCol>=0&&newCol<m_boardSize&&m_board[newRow][newCol]==role)
            {
                cnt++;
            }
            else {
                break;
            }
        }
        //向一个方向的反方向探测
        for(int idx=1;idx<5;idx++)
        {
             int newRow=row-directions[direct][0]*idx;
             int newCol=col-directions[direct][1]*idx;
            if(newRow>=0&&newRow<m_boardSize&&newCol>=0&&newCol<m_boardSize&&m_board[newRow][newCol]==role)
            {
                cnt++;
            }
            else {
                break;
            }
        }
        if(cnt>=5)
        {
            return true;
        }
    }
    return false;


}
void GameLogic::computeMove()
{
    int bestScore=0;
    int bestRow=0;
    int bestCol=0;
    qDebug()<<"我选择哪个位置呢"<<endl;
    for(int row=0;row<m_boardSize;row++)
    {
        for(int col=0;col<m_boardSize;col++)
        {
            if(m_board[row][col]==0)
            {
                /*判断这个点AI来说是不是最有利的*/
                /*对手最有利的点分最高，所以要加上对手和自己的最高分，也就是ai最优的下棋位置*/
              int score=evaluateMove(row,col,COMPUTER)+evaluateMove(row,col,PLATER);
                if(bestScore<score)
                {
                    bestScore=score;
                    bestRow=row;
                    bestCol=col;
                }
            }
        }
    }

    emit Aiposition(bestRow,bestCol);


}

int GameLogic::evaluateMove(int row, int col, Role role)
{
    int score=0;
    /*连子数+开放端*/
    /*四个方向*/
    const int directions[4][2]=
    {
        /*水平*/
        {1,0},
        /*垂直*/
        {0,1},
        /*左对角*/
        {1,1},
        /*右对角*/
        {1,-1}
    };
    /*四个方向探测*/
    for(int direct=0;direct<4;direct++)
    {
        /*连子数*/
        int count=0;
        /*开放端*/
        int openEnds=0;
        /*向一个方向检查最多四个棋子*/
        for(int idx=1;idx<=4;idx++)
        {
            int newRow=row+directions[direct][0]*idx;
            int newCol=col+directions[direct][1]*idx;
            /*检测新坐标是否在棋盘中*/
             if(newRow>=0&&newRow<m_boardSize&&newCol>=0&&newCol<m_boardSize)
             {
                 if(m_board[newRow][newCol]==role)
                 {
                     count++;
                 }
                 else if(m_board[newRow][newCol]==EMPTY)
                 {
                     openEnds++;
                     break;
                 }
                 else {
                     /*遇到对手棋子*/
                     break;
                 }

             }
             else {
                 break;
             }
        }
        /*向一个反方向检查最多四个棋子*/
        for(int idx=1;idx<=4;idx++)
        {
            int newRow=row-directions[direct][0]*idx;
            int newCol=col-directions[direct][1]*idx;
            /*检测新坐标是否在棋盘中*/
             if(newRow>=0&&newRow<m_boardSize&&newCol>=0&&newCol<m_boardSize)
             {
                 if(m_board[newRow][newCol]==role)
                 {
                     count++;
                 }
                 else if(m_board[newRow][newCol]==EMPTY)
                 {
                     openEnds++;
                     break;
                 }
                 else {
                     /*遇到对手棋子*/
                     break;
                 }

             }
             else {
                 break;
             }
        }
        if(count==4&&openEnds>0)
        {
            score+=10000;
        }
        else if(count==3&&openEnds>0)
        {
            score+=1000;
        }
        else if(count==2&&openEnds>0)
        {
            score+=100;
        }
        else if(count==1&&openEnds>0)
        {
            score+=10;
        }


    }
    return score;


}


